<?php
namespace App\Http\Controllers\Property;

use App\Common\json;
use App\Http\Controllers\Controller;
use App\Libraries\upload;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Common\push;


class  CommentController extends Controller{

    private $app_key = '09ae7f39eb877836195b52df';        //待发送的应用程序(appKey)，只能填一个。
    private $master_secret ='28783d0dacf52cfd12d952a6';    //主密码
    private $url = "https://api.jpush.cn/v3/push";      //推送的地址

    private $appios_key = '1aef5ddc66c9bc4e821e84a3';        //待发送的应用程序(appKey)，只能填一个。
    private $masterios_secret ='e0a01a1f2e56db54bf06f3fb';    //主密码
    private $urlios = "https://api.jpush.cn/v3/push";      //推送的地址*/

    private $apps_key = '72103a9a13805b96a0ed55b7';        //待发送的应用程序(appKey)，只能填一个。
    private $masters_secret ='590ea7bc6b3d916cab6d688a';    //主密码
    private $urls = "https://api.jpush.cn/v3/push";      //推送的地址

    private $appioss_key = '16f9437a241bd6bf2469b87d';        //待发送的应用程序(appKey)，只能填一个。
    private $masterioss_secret ='41c2ef1cfc66d759c8fdee9c';    //主密码
    private $urlioss = "https://api.jpush.cn/v3/push";      //推送的地址
    //回复评论
    public function addcomment(Request $request){
        if($request ->isMethod('post')){
            $ids         = $request ->input('id');
            $user_id    = $request ->input('user_id');  //用户ID
            $release_id  = $request ->input('release_id'); //主题ID
            $content     = $request ->input('comment');    //内容
            $userID      = $request ->input('userID');
            $type        = $request ->input('type');
            $time        = date('Y-m-d H:i:s');
/*            if(empty($ids)){
                $arrs = (object)array();
                return json::json('failure', '没有评论信息', $arrs);
            }*/
            if(empty($userID)){
                $userIDS      = 0;
            }else{
                $userIDS      = $userID;
            }
            $data = DB::table('release_topic')
                ->insert([
                    'user_releasetopic' => $user_id,
                    'release_releasetopic' => $release_id,
                    'releasetopic_content' => $content,
                    'userID'                =>$userIDS,
                    'operate_type'         =>$type,
                    'releasetopic_create_time' => $time
                ]);
            if($data == true){
               /* if(empty($userID)){*/
                $select = DB::table('release')->select('release_id')->where('user_release', $ids)->get();
                $arrs = (object)array();
                if (empty($select)) {
                    return json::json('success', '发布成功', $arrs);
                } else {
                    $arr = array();
                    foreach ($select as $value) {
                        $data = DB::table('release_topic')->select('releasetopic_id')->where('release_releasetopic', $value->release_id)->where('operate', 0)->get();
                        foreach ($data as $key) {
                            $id = $key->releasetopic_id;
                            $arr[] = $id;
                        }
                    }
                    $arlike = array();
                    foreach ($select as $value) {
                        $datalike = DB::table('like')->select('like_id')->where('release_like', $value->release_id)->where('operate', 0)->get();
                        foreach ($datalike as $key) {
                            $id = $key->like_id;
                            $arlike[] = $id;
                        }
                    }
                    $sumdata = count($arr) + count($arlike);

                    if ($sumdata == 0) {
                        return json::json('failure', '没有评论信息', $arrs);
                    } else {
                        try {
                            $datselect = DB::table('user')->select('phone')->where('user_id', $ids)->first();
                            $push = new push($this->app_key, $this->master_secret, $this->url);
                            $push->send_pub($datselect->phone, '你有' . $sumdata . '条新的评论', '8', $sumdata);
                            $pushios = new push($this->appios_key, $this->masterios_secret, $this->urlios);
                            $pushios->send_pub($datselect->phone, '你有' . $sumdata . '条新的评论', '8', $sumdata);
                            $push = new push($this->apps_key, $this->masters_secret, $this->urls);
                            $push->send_pub($datselect->phone, '你有' . $sumdata . '条新的评论', '8', $sumdata);
                            $pushios = new push($this->appioss_key, $this->masterioss_secret, $this->urlioss);
                            $pushios->send_pub($datselect->phone, '你有' . $sumdata . '条新的评论', '8', $sumdata);
                        }catch (\Exception $exception){
                            exit();
                        }
                    }}
               // }
                return json::json('success', '发布成功', json::value(1));
            }else{
                return json::json('failure','由于网络或者不正当原因失败',json::value(1));
            }
        }else{
            return json::json('101','提交方式错误','');
        }
    }

  /*  //回复评论
    public function addcomment(Request $request){
        if($request ->isMethod('post')){
            $user_id    = $request ->input('user_id');
            $release_id  = $request ->input('release_id');
            $content     = $request ->input('comment');
            $time        = date('Y-m-d H:i:s');
            $data        = DB::table('release_topic')
                ->insert([
                    'user_releasetopic'   => $user_id,
                    'release_releasetopic' => $release_id,
                    'releasetopic_content' => $content,
                    'releasetopic_create_time' =>$time
                ]);
            if($data == true){
                return json::json('success', '发布成功', json::value(1));
            }else{
                return json::json('failure','由于网络或者不正当原因失败',json::value(1));
            }
        }else{
            return json::json('101','提交方式错误','');
        }
    }*/

    //获取回复评论
    public function selectcomment(Request $request){
        if($request ->isMethod('post')){
            $user_id      = $request ->input('user_id');
            $data         = DB::table('release_topic')
                ->where('user_releasetopic',$user_id)
                ->get();
            if($data == true){
                return json::json('success', '获取回复评论成功', $data);
            }else{
                return json::json('failure','由于网络或者不正当原因失败',json::value(2));
            }
        }else{
            return json::json('101','提交方式错误','');
        }

    }

    //修改回复评论
    public function updatecomment(Request $request){
        if($request ->isMethod('post')){
            //  $user_id    = $request ->input('user_id');
            $releasetopic_id  = $request ->input('releasetopic_id');
            $content     = $request ->input('content');
            $time        = date('Y-m-d H:i:s');
            $data        = DB::table('release_topic')
                ->where('releasetopic_id',$releasetopic_id)
                ->update([
                    'releasetopic_content' => $content,
                    'releasetopic_update_time' =>$time
                ]);
            if($data == true){
                return json::json('success', '修改成功', json::value(1));
            }else{
                return json::json('failure','由于网络或者不正当原因失败',json::value(1));
            }
        }else{
            return json::json('101','提交方式错误','');
        }
    }

    //删除回复评论
    public function delcomment(Request $request){
        if($request ->isMethod('post')){
            $releasetopic_id  = $request ->input('comment_id');
            $data        = DB::table('release_topic')
                ->where('releasetopic_id',$releasetopic_id)
                ->delete();
            if($data == true){
                return json::json('success', '删除成功', json::value(1));
            }else{
                return json::json('failure','由于网络或者不正当原因失败',json::value(1));
            }
        }else{
            return json::json('101','提交方式错误','');
        }
    }



}